def assertAmount(a=float, b=float, jingdu=int):
    x = 0
    if b < 10 ** 4:
        x = 0
        jingdu = 0
    if b >= 10 ** 4 and b < 10 ** 8:
        x = 4
    elif b >= 10 ** 8:
        x = 8
    chu = 10.0 ** x
    fa = newRound(a / float(chu), jingdu)
    fb = newRound(b / float(chu), jingdu)

    if fa > fb:
        assert fa <= fb * 1.05, '[' + str(a) + '] != [' + str(b) + ']'
    elif fa < fb:
        assert fa >= fb * 0.95, '[' + str(a) + '] != [' + str(b) + ']'
    else:
        assert fa == fb, '[' + str(a) + '] != [' + str(b) + ']'


def assertMinFlaot(a=float, b=float):
    lessThan = b * 0.005
    if lessThan < 0.05:
        lessThan = 0.05

    fa = newRound(a, 2)
    fb = newRound(b, 2)
    difNum = newRound(abs(fa - fb), 2)
    assert difNum <= lessThan, '[' + str(a) + '] != [' + str(b) + ']'


def newRound(data, i=0):
    '''
    四舍五入,解决round(7.35)=7.3的问题
    :param data:
    :param i: 保留的位数,默认0
    :return:
    '''
    data = 0.0000 if data == None else float(data)

    if not isinstance(i, int):  # i不是整数
        raise TypeError('the param[i] must be int')
    else:
        mi = 10 ** i
        datax = data * mi
        f = datax - int(datax)
        if f >= 0.5:
            res = (int(datax) + 1) / mi
        elif f <= -0.5:
            res = (int(datax) - 1) / mi
        else:
            res = int(datax) / mi

        if i <= 0:
            res = int(res)
    return float('%.2f' % res)


def compareFloat(a=float, b=float, RoundPercent=0.01):
    fa = newRound(float(a), 2)
    fb = newRound(float(b), 2)
    if fa > fb:
        return fa <= fb * (1.00 + RoundPercent)
    elif fa < fb:
        return fa >= fb * (1.00 - RoundPercent)
    else:
        return fa == fb


def compareAmount(a=float, b=float, jingdu=int):
    x = 0
    if b < 10 ** 4:
        x = 0
        jingdu = 0
    if b >= 10 ** 4 and b < 10 ** 8:
        x = 4
    elif b >= 10 ** 8:
        x = 8
    chu = 10.0 ** x
    fa = newRound(a / float(chu), jingdu)
    fb = newRound(b / float(chu), jingdu)

    if fa > fb:
        return fa <= fb * 1.05
    elif fa < fb:
        return fa >= fb * 0.95
    else:
        return fa == fb

